package com.zb.excel.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

import java.util.Objects;

/**
 *
 * @Author WEN
 * @Date 2024/1/3 0003
 **/
public class UserExcelConverter implements Converter<Object> {
    @Override
    public Class<?> supportJavaTypeKey() {
        return Converter.super.supportJavaTypeKey();
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return Converter.super.supportExcelTypeKey();
    }

    @Override
    public Object convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return Converter.super.convertToJavaData(cellData, contentProperty, globalConfiguration);
    }

    @Override
    public Object convertToJavaData(ReadConverterContext<?> context) throws Exception {
        return Converter.super.convertToJavaData(context);
    }

    // 导出Excel数据类型转换
    @Override
    public WriteCellData<?> convertToExcelData(Object value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        String val = "";
        String fileName = contentProperty.getField().getName();
        if (Objects.equals("status", fileName)) {
            if (Objects.equals(0, value)) val = "正常";
            if (Objects.equals(1, value)) val = "冻结";
            if (Objects.equals(2, value)) val = "禁用";
        }
        if (Objects.equals("selectKnowledgeAuthor", fileName)) {
            val = null != value && (Boolean) value? "根据客户分类权限查看" : "查看所有";
        }
        if (Objects.equals("bindingWechat", fileName)) {
            val = null != value && (Boolean) value? "已绑定" : "未绑定";
        }
        return new WriteCellData<>(val);
    }

    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<Object> context) throws Exception {
        return Converter.super.convertToExcelData(context);
    }
}
